D:\a\csshw\csshw\src\serde\deserialization.rs
Line | Count | Source |
1 | | use windows::Win32::{ |
2 | | Foundation::BOOL, |
3 | | System::Console::{INPUT_RECORD_0, KEY_EVENT_RECORD, KEY_EVENT_RECORD_0}, |
4 | | }; |
5 | | |
6 | | use crate::serde::SERIALIZED_PID_LENGTH; |
7 | | |
8 | | /// Deserialize a [KEY_EVENT_RECORD_0] from a u8 slice using custom binary format. |
9 | | /// |
10 | | /// Tries to read a u16 from the given slice in little-endian format. |
11 | | /// |
12 | | /// Panics if reconstruction fails. |
13 | 1 | pub fn deserialize_key_event_record_0(slice: &[u8]) -> KEY_EVENT_RECORD_0 { |
14 | 1 | return KEY_EVENT_RECORD_0 { |
15 | 1 | UnicodeChar: u16::from_le_bytes([slice[0], slice[1]]), |
16 | 1 | }; |
17 | 1 | } |
18 | | |
19 | | /// Deserialize a [KEY_EVENT_RECORD] from a u8 slice using custom binary format. |
20 | | /// The slice is expected to be 13 bytes long. |
21 | | /// |
22 | | /// Layout: [1 byte KeyDown][2 bytes RepeatCount][2 bytes VirtualKeyCode] |
23 | | /// [2 bytes VirtualScanCode][2 bytes UnicodeChar][4 bytes ControlKeyState] |
24 | | /// |
25 | | /// Panics if reconstruction fails. |
26 | 2 | pub fn deserialize_key_event_record(slice: &[u8]) -> KEY_EVENT_RECORD { |
27 | 2 | return KEY_EVENT_RECORD { |
28 | 2 | // KeyDown (1 byte) |
29 | 2 | bKeyDown: BOOL::from(slice[0] != 0), |
30 | 2 | // RepeatCount (2 bytes LE) |
31 | 2 | wRepeatCount: u16::from_le_bytes([slice[1], slice[2]]), |
32 | 2 | // VirtualKeyCode (2 bytes LE) |
33 | 2 | wVirtualKeyCode: u16::from_le_bytes([slice[3], slice[4]]), |
34 | 2 | // VirtualScanCode (2 bytes LE) |
35 | 2 | wVirtualScanCode: u16::from_le_bytes([slice[5], slice[6]]), |
36 | 2 | // UnicodeChar (2 bytes LE) |
37 | 2 | uChar: KEY_EVENT_RECORD_0 { |
38 | 2 | UnicodeChar: u16::from_le_bytes([slice[7], slice[8]]), |
39 | 2 | }, |
40 | 2 | // ControlKeyState (4 bytes LE) |
41 | 2 | dwControlKeyState: u32::from_le_bytes([slice[9], slice[10], slice[11], slice[12]]), |
42 | 2 | }; |
43 | 2 | } |
44 | | |
45 | | /// Deserialize an [INPUT_RECORD_0].`KeyEvent` from a u8 slice using custom binary format. |
46 | | /// |
47 | | /// Panics if reconstruction fails. |
48 | 1 | pub fn deserialize_input_record_0(slice: &[u8]) -> INPUT_RECORD_0 { |
49 | 1 | let key_event = deserialize_key_event_record(slice); |
50 | 1 | return INPUT_RECORD_0 { |
51 | 1 | KeyEvent: key_event, |
52 | 1 | }; |
53 | 1 | } |
54 | | |
55 | | /// Deserialize a process id from its little-endian byte representation used |
56 | | /// by the named-pipe PID handshake. |
57 | 6 | pub fn deserialize_pid(bytes: &[u8; SERIALIZED_PID_LENGTH]) -> u32 { |
58 | 6 | return u32::from_le_bytes(*bytes); |
59 | 6 | } |